<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HEAD request (Client) 💡</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast">
<link rel="up" href="../more_examples.html" title="HTTP Examples">
<link rel="prev" href="expect_100_continue_server.html" title="Expect 100-continue (Server) 💡">
<link rel="next" href="head_response_server.html" title="HEAD response (Server) 💡">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="expect_100_continue_server.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../more_examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="head_response_server.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="beast.more_examples.head_request_client"></a><a class="link" href="head_request_client.html" title="HEAD request (Client) 💡">HEAD request
      (Client) 💡</a>
</h3></div></div></div>
<p>
        The <a href="https://tools.ietf.org/html/rfc7231#section-4.3.2" target="_top">HEAD request</a>
        method indicates to the server that the client wishes to receive the entire
        header that would be delivered if the method was GET, except that the body
        is omitted. When a client wishes to receive the response to a HEAD request,
        it is necessary to inform the parser not to expect a body. This is done by
        calling <a class="link" href="../ref/boost__beast__http__basic_parser/skip.html" title="http::basic_parser::skip"><code class="computeroutput"><span class="identifier">basic_parser</span><span class="special">::</span><span class="identifier">skip</span></code></a> with the value <code class="computeroutput"><span class="keyword">true</span></code>, as shown in this example:
      </p>
<pre class="programlisting"><span class="comment">/** Send a HEAD request for a resource.

    This function submits a HEAD request for the specified resource
    and returns the response.

    @param res The response. This is an output parameter.

    @param stream The synchronous stream to use.

    @param buffer The buffer to use.

    @param target The request target.

    @param ec Set to the error, if any occurred.

    @throws std::invalid_argument if target is empty.
*/</span>
<span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">class</span> <span class="identifier">SyncStream</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">DynamicBuffer</span>
<span class="special">&gt;</span>
<span class="identifier">response</span><span class="special">&lt;</span><span class="identifier">empty_body</span><span class="special">&gt;</span>
<span class="identifier">do_head_request</span><span class="special">(</span>
    <span class="identifier">SyncStream</span><span class="special">&amp;</span> <span class="identifier">stream</span><span class="special">,</span>
    <span class="identifier">DynamicBuffer</span><span class="special">&amp;</span> <span class="identifier">buffer</span><span class="special">,</span>
    <span class="identifier">string_view</span> <span class="identifier">target</span><span class="special">,</span>
    <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">)</span>
<span class="special">{</span>
    <span class="comment">// Do some type checking to be a good citizen</span>
    <span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">is_sync_stream</span><span class="special">&lt;</span><span class="identifier">SyncStream</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">,</span>
        <span class="string">"SyncStream requirements not met"</span><span class="special">);</span>
    <span class="keyword">static_assert</span><span class="special">(</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">is_dynamic_buffer</span><span class="special">&lt;</span><span class="identifier">DynamicBuffer</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">,</span>
        <span class="string">"DynamicBuffer requirements not met"</span><span class="special">);</span>

    <span class="comment">// The interfaces we are using are low level and do not</span>
    <span class="comment">// perform any checking of arguments; so we do it here.</span>
    <span class="keyword">if</span><span class="special">(</span><span class="identifier">target</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span>
        <span class="keyword">throw</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">invalid_argument</span><span class="special">(</span><span class="string">"target may not be empty"</span><span class="special">);</span>

    <span class="comment">// Build the HEAD request for the target</span>
    <span class="identifier">request</span><span class="special">&lt;</span><span class="identifier">empty_body</span><span class="special">&gt;</span> <span class="identifier">req</span><span class="special">;</span>
    <span class="identifier">req</span><span class="special">.</span><span class="identifier">version</span><span class="special">(</span><span class="number">11</span><span class="special">);</span>
    <span class="identifier">req</span><span class="special">.</span><span class="identifier">method</span><span class="special">(</span><span class="identifier">verb</span><span class="special">::</span><span class="identifier">head</span><span class="special">);</span>
    <span class="identifier">req</span><span class="special">.</span><span class="identifier">target</span><span class="special">(</span><span class="identifier">target</span><span class="special">);</span>
    <span class="identifier">req</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">user_agent</span><span class="special">,</span> <span class="string">"test"</span><span class="special">);</span>

    <span class="comment">// A client MUST send a Host header field in all HTTP/1.1 request messages.</span>
    <span class="comment">// https://tools.ietf.org/html/rfc7230#section-5.4</span>
    <span class="identifier">req</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">host</span><span class="special">,</span> <span class="string">"localhost"</span><span class="special">);</span>

    <span class="comment">// Now send it</span>
    <span class="identifier">write</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">req</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span>
    <span class="keyword">if</span><span class="special">(</span><span class="identifier">ec</span><span class="special">)</span>
        <span class="keyword">return</span> <span class="special">{};</span>

    <span class="comment">// Create a parser to read the response.</span>
    <span class="comment">// We use the `empty_body` type since</span>
    <span class="comment">// a response to a HEAD request MUST NOT</span>
    <span class="comment">// include a body.</span>
    <span class="identifier">response_parser</span><span class="special">&lt;</span><span class="identifier">empty_body</span><span class="special">&gt;</span> <span class="identifier">p</span><span class="special">;</span>

    <span class="comment">// Inform the parser that there will be no body.</span>
    <span class="identifier">p</span><span class="special">.</span><span class="identifier">skip</span><span class="special">(</span><span class="keyword">true</span><span class="special">);</span>

    <span class="comment">// Read the message. Even though fields like</span>
    <span class="comment">// Content-Length or Transfer-Encoding may be</span>
    <span class="comment">// set, the message will not contain a body.</span>
    <span class="identifier">read</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">buffer</span><span class="special">,</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span>
    <span class="keyword">if</span><span class="special">(</span><span class="identifier">ec</span><span class="special">)</span>
        <span class="keyword">return</span> <span class="special">{};</span>

    <span class="comment">// Transfer ownership of the response to the caller.</span>
    <span class="keyword">return</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">release</span><span class="special">();</span>
<span class="special">}</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
      Falco<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="expect_100_continue_server.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../more_examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="head_response_server.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
